using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	#endregion

	public partial class FrmChonChoNgoi : Office2007Form
	{
		public EventHandler Handler { get; set; }
        public ChiTietLichHoatDong ChiTiet { get; set; }
        private int SelectedChoNgoi = -1;

        public FrmChonChoNgoi(int machuyenxe = -1)
		{
            InitializeComponent();
		}

        private void FrmChonChoNgoi_Load(object sender, EventArgs e)
        {
            InitDays();
            DisableGhes();
            DisableGheDaDat();
        }

        /// <summary>
        /// Disable ghe da duoc dat
        /// </summary>
        private void DisableGheDaDat()
        {
            var soGheTrenXe = this.ChiTiet.LichHoatDong.ChuyenXe.Xe.SoGhe.Value;
            for (int i = 0; i < soGheTrenXe; i++)
            {
                // Get ve dat dat 
                var ves = VeRepository.SelectByMaChiTietLichHoatDong(this.ChiTiet.MaChiTietLichHoatDong);
                var ghe = ves.FirstOrDefault(n => n.SoGhe == (i + 1));
                if (ghe != null)
                {
                    var arrCkbx = groupBox1.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                    if (arrCkbx.Count() > 0)
                    {
                        arrCkbx[0].Enabled = false;
                    }
                    else
                    {
                        arrCkbx = groupBox2.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                        if (arrCkbx.Count() > 0)
                        {
                            arrCkbx[0].Enabled = false;
                        }
                        else
                        {
                            arrCkbx = groupBox3.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                            if (arrCkbx.Count() > 0)
                            {
                                arrCkbx[0].Enabled = false;
                            }
                            else
                            {
                                arrCkbx = groupBox4.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                                if (arrCkbx.Count() > 0)
                                {
                                    arrCkbx[0].Enabled = false;
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Chi hien thi so luong ghe dua vao so ghe tren xe
        /// </summary>
        private void DisableGhes()
        {
            var soGheTrenXe = this.ChiTiet.LichHoatDong.ChuyenXe.Xe.SoGhe.Value;
            if (soGheTrenXe < 48)
            {
                for (int i = soGheTrenXe; i < 48; i++)
                {
                    var arrCkbx = groupBox1.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                    if (arrCkbx.Count() > 0)
                    {
                        arrCkbx[0].Enabled = false;
                    }
                    else
                    {
                        arrCkbx = groupBox2.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                        if (arrCkbx.Count() > 0)
                        {
                            arrCkbx[0].Enabled = false;
                        }
                        else
                        {
                            arrCkbx = groupBox3.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                            if (arrCkbx.Count() > 0)
                            {
                                arrCkbx[0].Enabled = false;
                            }
                            else
                            {
                                arrCkbx = groupBox4.Controls.Find(string.Format("ckbxGhe{0}", (i + 1).ToString()), false);
                                if (arrCkbx.Count() > 0)
                                {
                                    arrCkbx[0].Enabled = false;
                                }
                            }
                        }
                    }
                }
            }
        }

        private void InitDays()
        {
            // Day 1
            var X = 10;
            var Y = 10;
            var OFFSET = 23;
            var Width = 60;

            for (int i = 0; i < 48; i++)
            {
                if (i % 12 == 0)
                    Y = 10;

                RadioButton chkBox = new RadioButton();
                chkBox.Name = string.Format("ckbxGhe{0}", i + 1);
                chkBox.Text = string.Format("Ghế {0}", i + 1);
                chkBox.Checked = false;
                chkBox.Tag = i + 1;
                chkBox.Location = new Point(X, Y);
                chkBox.Width = Width;
                chkBox.Click += radio_Click;

                if(i < 12)
                    groupBox1.Controls.Add(chkBox);
                else if (i < 24)
                    groupBox2.Controls.Add(chkBox);
                else if (i < 36)
                    groupBox3.Controls.Add(chkBox);
                else if (i < 48)
                    groupBox4.Controls.Add(chkBox);

                Y += OFFSET;
            }
        }

        void radio_Click(object sender, EventArgs e)
        {
            SelectedChoNgoi = (int)((RadioButton)sender).Tag;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if(SelectedChoNgoi == -1)
            {
                MessageBoxEx.Show("Bạn chưa chọn chỗ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Handler(sender, new TransferSimpleDataEvent(SelectedChoNgoi.ToString()));
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupBox2.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox3.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox4.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupBox1.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox3.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox4.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupBox1.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox2.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox4.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }
        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupBox1.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox2.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }

            foreach (Control ctrl in groupBox3.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).Checked = false;
            }
        }
	}
}
